<template>
  <div>
    <!-- 面包屑视图 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">
            添加角色
          </el-button>
        </el-col>
      </el-row>
      <el-table
        :data="roleData"
        style="width: 100%"
        border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item,i) in scope.row.children" :class="['one-row','bottom-border',i===0?'top-border':'']"
                    :key="item.id">
              <el-col :span="5">
                <el-tag type="primary" closable @close="deleteRoleByid(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2,i2) in item.children" :key="item2.id" :class="['my-row',i2===0?'':'top-border']">
                  <el-col :span="6">
                    <el-tag type="danger" closable @close="deleteRoleByid(scope.row,item2.id)">{{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag type="success" v-for="item3 in item2.children" :key="item3.id" closable
                            @close="deleteRoleByid(scope.row,item3.id)">
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName">
        </el-table-column>
        <el-table-column
          label="角色描述"
          prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作" width="350">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
            <!--授权弹窗开启按钮-->
            <el-button type="warning" icon="el-icon-setting" @click="settingRights(scope)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--授权弹窗-->
      <el-dialog
        title="角色所拥有权限"
        :visible.sync="addRoleDialogVisible"
        width="40%"
        >
        <el-tree
          :props="props"
          :data="treeData"
          show-checkbox
          default-expand-all
          :default-checked-keys="defaultKeys"
          node-key="id"
          ref="roleTree"
          >
        </el-tree>
        <span slot="footer" class="dialog-footer">
              <el-button @click="addRoleDialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="giveRoleRights">确 定</el-button>
            </span>
      </el-dialog>
    </el-card>

  </div>
</template>
<script>
export default {
  created () {
    this.getRoleList()
  },
  data () {
    return {
      roleData: [],
      addRoleDialogVisible: false,
      treeData: [],
      props: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中节点的数组
      defaultKeys: [],
      // 授权弹窗那一行的数据
      theRowList: {}
    }
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.roleData = res.data
    },
    async deleteRoleByid (role, rightId) {
      const confirm = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (confirm === 'confirm') {
        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        role.children = res.data
        this.$message.success('删除成功')
      }
    },
    // 打开授权弹框 并完成一些数据的初始化获取
    async settingRights (var1) {
      this.theRowList = []
      this.theRowList = var1.row
      this.addRoleDialogVisible = true
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.treeData = res.data
      // 获取选中节点数组并绑定到defaultKeys数组中去
      await this.getDefaultCheckedKeys(var1.row.children)
      this.$message.success('操作成功')
    },
    // 获取默认选中的key组成的集合
    getDefaultCheckedKeys (var2) {
      this.defaultKeys = []
      var2.forEach(item => {
        item.children.forEach(item => {
          item.children.forEach(item => {
            this.defaultKeys.push(item.id)
          })
        })
      })
    },
    // 获取一级节点 二级节点 三级节点 和半选中节点组成的字符串
    async giveRoleRights () {
      const keys = this.$refs.roleTree.getCheckedKeys()
      const keys2 = this.$refs.roleTree.getHalfCheckedKeys()
      const sumkeys = [...keys, ...keys2]
      const s = sumkeys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.theRowList.id}/rights`, { rids: s })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.addRoleDialogVisible = false
      this.getRoleList()
      this.$message.success(res.meta.msg)
    }
  }
}
</script>
<style lang="less" scoped>
  .my-row {
    padding-top: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .one-row {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 !important;
  }

  .top-border {
    border-top: 1px solid #cecece;
  }

  .bottom-border {
    border-bottom: 1px solid #cecece;
  }

  .el-tag {
    margin: 10px 5px;
  }
</style>
